﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

using MicroCenter.Models;
using MicroCenter.Services;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;

using Utils;

namespace MicroCenter.Hubs
{
    [Route("api/ws/[hub]")]
    public class MicroHub : Hub
    {
        private readonly MicroService microService;

        public MicroHub(MicroService microService)
        {
            this.microService = microService;
        }

        public override Task OnConnectedAsync()
        {

            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            microService.Remove(Context.ConnectionId);
            return base.OnDisconnectedAsync(exception);
        }

        public void Registry(RegistryModel model)
        {
            var context = Context.GetHttpContext();
            if (!microService.CheckAccessToken(context))
            {
                Context.Abort();
                Dispose();
                return;
            }
            model.RemoteIP = context.Connection.RemoteIpAddress.ToString();
            microService.Registry(Context.ConnectionId, model);
        }


        public RegistryModel[] GetRegistries()
        {
            return microService.GetRegistries();
        }
    }
}
